﻿Imports System.Text.RegularExpressions

Public Class RegExHelper
    Public Shared Function GetRegExMatches(ByVal inputText As String, ByVal searchText As String) As MatchCollection
        If String.IsNullOrWhiteSpace(searchText) Then
            Return Nothing
        End If
        Dim lSafeSearchText As String
        'Break apart the search into string array split by one or more space
        Dim lWords() As String = Regex.Split(searchText, "\s+")
        'Join the words back togeter to form a new RegEx with the escaped words separate by the OR operator (|)
        Dim sb As New System.Text.StringBuilder
        For Each lWord As String In lWords
            'Escape the word in case there are any RegEx metacharacters in the string
            sb.Append(Regex.Escape(lWord))
            'Build OR
            sb.Append("|")
        Next lWord
        If sb.Length > 0 Then
            'Convert to string stripping the extra |
            lSafeSearchText = sb.ToString(0, sb.Length - 1)
            Return Regex.Matches(inputText, lSafeSearchText, RegexOptions.IgnoreCase)
        Else
            Return Nothing
        End If
    End Function
End Class
